<template>
  <div class="overview">
    <!-- 轮播 -->
    <div class="scroll-wrapper">
      <div class="scroll-con">
        <div
          v-for="(item, index) in systemTypeName"
          :key="index"
          :class="item.select?'item selected':'item'"
          @click="seleFlowOverview(item.id)"
        >
          <div class="item-data">
            <span>{{ item.name }}</span>
            <span>{{ $t('dataAnalysis.numberOfVisitors') }}</span>
          </div>
          <div class="item-data">
            <span class="number">{{ item.userNum }}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 轮播 -->

    <!-- 数据展示 -->
    <div class="show-data">
      <div class="data-board data-board-left">
        <div class="data-board__title visit-title">
          <div>
            <img
              src="~@/assets/img/visit.png"
              style="margin-bottom: 10px"
              alt
            >
            <p>{{ $t('dataAnalysis.pageBrowsing1') }}</p>
          </div>
        </div>
        <div class="data-board__content visit-content">
          <div class="data-board__content-item_box flex-box1">
            <div class="data-board__content-item">
              <div class="data-board__content-item_tittle">
                {{ $t('dataAnalysis.numberOfVisitors') }}
              </div>
              <div class="num">
                {{ dataArray.userNums }}
              </div>
              <div
                v-if="timeType!==4"
                class="compare"
              >
                {{ typeName }}
                <div
                  v-if="ratio"
                  :class="['lift-rate', ratio.userNumsRatioRise ===false ? 'down' : 'rise']"
                >
                  <img
                    v-if="ratio.userNumsRatioRise===false"
                    src="~@/assets/img/downArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <img
                    v-else
                    src="~@/assets/img/upArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <span class="div">{{ Math.abs(ratio.userNumsRatio) }}%</span>
                </div>
              </div>
            </div>

            <div
              v-if="timeType!==4"
              class="data-board__content-item"
            >
              <div class="data-board__content-item_tittle">
                {{ $t('dataAnalysis.stayTime') }}
              </div>
              <div class="num">
                {{ dataArray.averageStopTime }}
              </div>
              <div class="compare">
                {{ typeName }}
                <div
                  v-if="ratio"
                  :class="['lift-rate', ratio.averageStopTimeRatioRise===false ? 'down' : 'rise']"
                >
                  <img
                    v-if="ratio.averageStopTimeRatioRise===false"
                    src="~@/assets/img/downArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <img
                    v-else
                    src="~@/assets/img/upArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <span class="div">{{ Math.abs(ratio.averageStopTimeRatio) }}%</span>
                </div>
              </div>
            </div>
          </div>

          <div class="data-board__content-item_box flex-box1">
            <div
              v-if="timeType!==4"
              class="data-board__content-item"
            >
              <div class="data-board__content-item_tittle">
                {{ $t('dataAnalysis.newVisitor') }}
              </div>
              <div class="num">
                {{ dataArray.newUser }}
              </div>
              <div class="compare">
                {{ typeName }}
                <div
                  v-if="ratio"
                  :class="['lift-rate', ratio.newUserRatioRise===false ? 'down' : 'rise']"
                >
                  <img
                    v-if="ratio.newUserRatioRise===false"
                    src="~@/assets/img/downArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <img
                    v-else
                    src="~@/assets/img/upArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <span class="div">{{ Math.abs(ratio.newUserRatio) }}%</span>
                </div>
              </div>
            </div>

            <div class="data-board__content-item">
              <div class="data-board__content-item_tittle">
                {{ $t('dataAnalysis.perVisits') }}
              </div>
              <div class="num">
                {{ dataArray.averageVisitNums }}
              </div>
              <div
                v-if="timeType!==4"
                class="compare"
              >
                {{ typeName }}
                <div
                  v-if="ratio"
                  :class="['lift-rate', ratio.averageVisitNumsRatioRise===false ? 'down' : 'rise']"
                >
                  <img
                    v-if="ratio.averageVisitNumsRatioRise===false"
                    src="~@/assets/img/downArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <img
                    v-else
                    src="~@/assets/img/upArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <span class="percentage">{{ Math.abs(ratio.averageVisitNumsRatio) }}%</span>
                </div>
              </div>
            </div>
          </div>

          <div class="data-board__content-item_box flex-box3">
            <div class="data-board__content-item">
              <div class="data-board__content-item_tittle">
                {{ $t('dataAnalysis.views') }}
              </div>
              <div class="num">
                {{ dataArray.visitNums }}
              </div>
              <div
                v-if="timeType!==4"
                class="compare"
              >
                {{ typeName }}
                <div
                  v-if="ratio"
                  :class="['lift-rate', ratio.visitNumsRatioRise===false ? 'down' : 'rise']"
                >
                  <img
                    v-if="ratio.visitNumsRatioRise===false"
                    src="~@/assets/img/downArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <img
                    v-else
                    src="~@/assets/img/upArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <span class="percentage">{{ Math.abs(ratio.visitNumsRatio) }}%</span>
                </div>
              </div>
            </div>

            <div
              v-if="timeType!==4"
              class="data-board__content-item"
            >
              <div class="data-board__content-item_tittle">
                {{ $t('dataAnalysis.lossRate') }}
              </div>
              <div class="num">
                {{ dataArray.lossUserRate }}%
              </div>
              <div class="compare">
                {{ typeName }}
                <div
                  v-if="ratio"
                  :class="['lift-rate', ratio.lossUserRatioRise===false ? 'down' : 'rise']"
                >
                  <img
                    v-if="ratio.lossUserRatioRise===false"
                    src="~@/assets/img/downArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <img
                    v-else
                    src="~@/assets/img/upArrow@2x.png"
                    class="arrows"
                    alt
                  >
                  <span class="percentage">{{ Math.abs(ratio.lossUserRatio) }}%</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="data-board data-board-right">
        <div class="data-board__title deal-title">
          <div>
            <img
              src="~@/assets/img/deal.png"
              style="margin-bottom: 10px"
              alt
            >
            <p>{{ $t('dataAnalysis.dealTrans') }}</p>
          </div>
        </div>

        <div class="data-board__content deal-content">
          <div class="data-board__content-item">
            <div class="data-board__content-item_tittle">
              {{ $t('dataAnalysis.paidNumbers') }}
            </div>
            <div class="num">
              {{ dataArray.payUser }}
            </div>
            <div
              v-if="timeType!==4"
              class="compare"
            >
              {{ typeName }}
              <div
                v-if="ratio"
                :class="['lift-rate', ratio.payUserRatioRise===false ? 'down' : 'rise']"
              >
                <img
                  v-if="ratio.payUserRatioRise===false"
                  src="~@/assets/img/downArrow@2x.png"
                  class="arrows"
                  alt
                >
                <img
                  v-else
                  src="~@/assets/img/upArrow@2x.png"
                  class="arrows"
                  alt
                >
                <span class="percentage">{{ Math.abs(ratio.payUserRatio) }}%</span>
              </div>
            </div>
          </div>
          <div
            v-if="timeType!==4"
            class="data-board__content-item"
          >
            <div class="data-board__content-item_tittle">
              {{ $t('dataAnalysis.vToP') }}
            </div>
            <div class="num">
              {{ dataArray.payRate }}%
            </div>
            <div class="compare">
              {{ typeName }}
              <div
                v-if="ratio"
                :class="['lift-rate', ratio.payRateRatioRise===false ? 'down' : 'rise']"
              >
                <img
                  v-if="ratio.payRateRatioRise===false"
                  src="~@/assets/img/downArrow@2x.png"
                  class="arrows"
                  alt
                >
                <img
                  v-else
                  src="~@/assets/img/upArrow@2x.png"
                  class="arrows"
                  alt
                >
                <span class="percentage">{{ Math.abs(ratio.payRateRatio) }}%</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 数据展示 -->
  </div>
</template>

<script setup>
const typeName = $t('dataAnalysis.compareWithThePreviousOneDay')
const props = defineProps({
  timeType: {
    default: 0,
    type: Number
  },
  startTime: {
    default: '',
    type: String
  }
})

onMounted(() => {
  getAnalysisData()
})

watch(() => props.startTime, () => {
  getAnalysisData()
})

const systemType = ref(0) // 系统类型
watch(() => systemType.value, val => {
  systemType.value = val
  getAnalysisData()
})

const ratio = ref({})
const ratioInit = () => {
  ratio.value = {
    averageStopTimeRatio: 0,
    averageStopTimeRatioRise: true,
    averageVisitNumsRatio: 0,
    averageVisitNumsRatioRise: true,
    lossUserRatio: 0,
    lossUserRatioRise: true,
    newUserRatio: 0,
    newUserRatioRise: true,
    payRateRatio: 0,
    payRateRatioRise: true,
    payUserRatio: 0,
    payUserRatioRise: true,
    userNumsRatio: 0,
    userNumsRatioRise: true,
    visitNumsRatio: 0,
    visitNumsRatioRise: true
  }
}
/**
 * 请求接口
 */
const systemTypeName = reactive([
  { name: $t('date.a'), id: 0, userNum: 0, select: true },
  { name: 'PC', id: 1, userNum: 0, select: false },
  { name: 'H5', id: 2, userNum: 0, select: false },
  { name: $t('sysManagement.miniPrograms'), id: 3, userNum: 0, select: false },
  { name: $t('dataAnalysis.android'), id: 4, userNum: 0, select: false },
  { name: 'IOS', id: 5, userNum: 0, select: false }
])
const dataArray = ref({
  userNums: 0,
  newUser: 0,
  visitNums: 0,
  averageStopTime: 0,
  averageVisitNums: 0,
  lossUserRate: '0.00%',
  payUser: 0,
  payRate: '0.00%'
})
const getAnalysisData = () => {
  // 请求第一部分数据
  ratioInit()
  http({
    url: http.adornUrl('/admin/flowAnalysis/getAnalysisData'),
    method: 'get',
    params: http.adornParams({
      timeType: props.timeType,
      start: !props.startTime ? null : props.startTime,
      systemType: systemType.value
    })
  }).then(({ data }) => {
    if (data) {
      dataArray.value = data
      ratio.value = data.ratio
    }
  })

  // 请求第一部分所有系统的访客数
  http({
    url: http.adornUrl('/admin/flowAnalysis/systemTypeNums'),
    method: 'get',
    params: http.adornParams({
      timeType: props.timeType,
      start: !props.startTime ? null : props.startTime
    })
  }).then(({ data }) => {
    if (data) {
      for (let i = 0; i < systemTypeName.length; i++) {
        if (i === 0) {
          systemTypeName[i].userNum = data.all
        } else if (i === 1) {
          systemTypeName[i].userNum = data.pc
        } else if (i === 2) {
          systemTypeName[i].userNum = data.h5
        } else if (i === 3) {
          systemTypeName[i].userNum = data.applets
        } else if (i === 4) {
          systemTypeName[i].userNum = data.android
        } else if (i === 5) {
          systemTypeName[i].userNum = data.ios
        }

        if (systemType.value === i) systemTypeName[i].select = true
        systemTypeName[i].select = false
      }
    }
  })
}

const emit = defineEmits(['onSystemTypeChange'])
// 流量总览点击切换
const seleFlowOverview = (systemTypes) => {
  systemType.value = systemTypes
  emit('onSystemTypeChange', systemTypes)
  getAnalysisData()
}

</script>

<style lang="scss" scoped>
 @use '../index.scss';
.overview {
  /* 轮播 */
  .scroll-con {
    padding: 10px 0;
    white-space: nowrap;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-pack: start;
    justify-content: flex-start;
    margin: -10px 0;
    flex-wrap: wrap;
  }

  .scroll-con .item {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 92px;
    min-width: 200px;
    max-width: 240px;
    border: 1px solid #dcdee0;
    border-radius: 4px;
    padding: 9px 16px 11px 16px;
    box-sizing: border-box;
    -ms-flex: 1 1 200px;
    flex: 1 1 200px;
    cursor: pointer;
    transition: box-shadow 0.2s ease 0s;
    margin: 8px 8px;
  }

  .scroll-con .item:hover {
    box-shadow: 0 2px 8px rgba(200, 201, 204, 0.5);
  }

  .scroll-con .selected {
    background-color: #edf4ff;
    border-color: #155bd4;
  }

  .item-data {
    display: flex;
    -ms-flex-pack: justify;
    justify-content: space-between;
  }

  .item-data .number {
    font-size: 25px;
    font-weight: bold;
    color: #333333;
  }

  /* 轮播 */

  /* 数据展示 */
  .show-data {
    display: flex;
    margin-top: 30px;
  }

  .show-data .data-board {
    display: flex;
    margin-bottom: 4px;
  }
  .data-board-left {
    width:60%;
    margin-right:10px
  }
  .data-board-right {
    width:40%
  }

  .flex-box1{
    flex: 2;
  }
  .flex-box2 .data-board__content-item {
    padding-left: 0;
    margin-left: -3%;
  }
  .flex-box3{
    flex: 2;
  }

  .data-board__content-item_tittle {
    font-size: 14px;
    font-weight: 400;
    color: #333333;
  }

  .data-board .data-board__title {
    padding: 0 12px;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-align: center;
    align-items: center;
    -ms-flex-pack: center;
    justify-content: center;
    color: #fff;
    text-align: center;
    -ms-flex: none;
    flex: none;
  }

  .data-board .visit-title {
    background-color: #155bd4;
  }

  .data-board .deal-title {
    background-color: #155bd4;
  }

  .data-board .data-board__content {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    flex: 1 1 auto;
    padding: 0 8px;
  }

  .data-board .visit-content {
    background-color: rgb(243, 247, 253);
  }

  .data-board .deal-content {
    background-color: #f3f7fd;
  }

  .data-board__content-item_box {
    display: flex;
    flex-direction: column;
  }
  .data-board__content-item {
    // min-width: 200px;
    // width: 33%;
    padding: 35px;
  }

  .data-board__content-item .num {
      font-size: 18px;
      font-weight: bold;
      color: #333333;
      opacity: 1;
      margin-top: 9px;
      margin-bottom: 12px;
  }

  .data-board__content-item .compare {
    color: #969799;
    margin-top: 8px;
    display: flex;
  }

  /* 数据展示 */
}

.lift-rate {
  display: flex;
  align-items: center;
  font-size: 12px;
  margin-left: 16px;
  .arrows {
    display: block;
    width: 8px;
    height: 10px;
    margin-right: 3px;
  }
}
.lift-rate.down .percentage {
  color: #FF4141;
  line-height: 1em;
}
.lift-rate.rise .percentage {
  color: #3CC480;
  line-height: 1em;
}
</style>
